package com.ambow.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ambow.entity.Message;
import com.ambow.entity.User;
import com.ambow.service.MessageService;
import com.ambow.service.UserService;
import com.ambow.service.impl.MessageServiceImpl;
import com.ambow.service.impl.UserServiceImpl;
import com.ambow.utils.PageResults;

/**
 * Servlet implementation class LeaveMessageServlet
 */
@WebServlet("/LeaveMessageServlet")
public class LeaveMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MessageService mService=new MessageServiceImpl();
    UserService userService  = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String status = request.getParameter("status");
       if(status.equals("add")) {
    	   add(request,response);
       } else if(status.equals("all")) {
    	   queryAll(request,response,status);
       }
		
	}

	//查询全部
	private void queryAll(HttpServletRequest request, HttpServletResponse response,String status) {
		try {
		
		String hql = "from Message where status=1";
		String countHql = "select count(*) from Message where status=1";
		Integer pageNo = Integer.valueOf(request.getParameter("currentPage"));
		Integer pageSize = 3;
		PageResults<Message> pageResults = mService.getPageByFetchedHql(hql, countHql, pageNo, pageSize);
		request.setAttribute("pageResults", pageResults);	
		request.setAttribute("status", status);	
		
		
		System.out.println(pageResults.getTotalCount()+"ffffffffff");
		request.getRequestDispatcher("pages/contact.jsp").forward(request, response);
		} catch (Exception e) {
		}
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title=request.getParameter("title"); //留言标题 
        String content=request.getParameter("message");//留言内容
        User user = (User) request.getSession().getAttribute("login");
        if(user!=null) {
        	Message mb=new Message();
            mb.setTitle(title);
            mb.setContent(content);
            mb.setCreateTime(new Date());
    		mb.setStatus(1);
    		mb.setUser(user);	
    		boolean b=mService.saveMessage(mb);
    		if(b) {
    			  List<Message> messages=mService.selectAllMessage(); 		
    			request.setAttribute("messages", messages);			
    			request.getRequestDispatcher("pages/contact.jsp").forward(request, response);
    		}
        }else {
        	
        }
	}

}
